layui.define(['form', 'admin', 'upload'], function (exports) {
    var $ = layui.$
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    form.on('submit(configpri-save)', function (obj) {
        //提交
        layer.load();
        admin.req({
            url: '/admin/setting/savepri'
            , data: obj.field
            , success: function (res) {
                layer.closeAll('loading');
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        window.location.reload();
                    }, 1200);
                }
            }
            , complete: function () {
                layer.closeAll("loading");
            }
        });
        return false;
    });

    form.on('submit(configpub-save)', function (obj) {
        //提交
        layer.load();
        admin.req({
            url: '/admin/setting/savepub'
            , data: obj.field
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    if (res.data.length > 0) {
                        var sourceKey = res.data[0].source_key;
                        var isNew = res.data[0].is_new;
                        if (sourceKey == 'pub' || isNew) {
                            setTimeout(function () {
                                parent.frames['source'].location.reload();
                            }, 1200);
                        }
                    }
                }
            }
            , complete: function () {
                layer.closeAll("loading");
            }
        });
        return false;
    });

    form.on('submit(configpub-sem-save)', function (obj) {
        //提交
        layer.load();
        admin.req({
            url: '/admin/setting/savepub_sem'
            , data: obj.field
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        window.location.reload();
                    }, 1200);
                }
            }
            , complete: function () {
                layer.closeAll("loading");
            }
        });
        return false;
    });

    form.on('submit(configpub-sem-web-save)', function (obj) {
        //提交
        layer.load();
        admin.req({
            url: '/admin/setting/savepub_sem_web'
            , data: obj.field
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        window.location.reload();
                    }, 1200);
                }
            }
            , complete: function () {
                layer.closeAll("loading");
            }
        });
        return false;
    });

    form.on('submit(configpub-spec-save)', function (obj) {
        //提交
        layer.load();
        admin.req({
            url: '/admin/setting/savepub_spec'
            , data: obj.field
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        window.location.reload();
                    }, 1200);
                }
            }
            , complete: function () {
                layer.closeAll("loading");
            }
        });
        return false;
    });

    form.on('submit(configpub-joycircle-save)', function (obj) {
        //提交
        layer.load();
        admin.req({
            url: '/admin/setting/savepub_joycircle'
            , data: obj.field
            , success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        window.location.reload();
                    }, 1200);
                }
            }
            , complete: function () {
                layer.closeAll("loading");
            }
        });
        return false;
    });


    form.on('switch(switch_shortvideo_check)', function (data) {
        $("input[name=switch_shortvideo_check]").val(this.checked ? '1' : '0');
    });

    form.on('switch(switch_moment_check)', function (data) {
        $("input[name=switch_moment_check]").val(this.checked ? '1' : '0');
    });

    form.on('switch(switch_iap)', function (data) {
        $("input[name=switch_iap]").val(this.checked ? '1' : '0');
    });

    //上传qrcode
    var qrcodeSrc = $('#LAY_qrcodeSrc');
    upload.render({
        url: '/admin/index/noOperation'
        , elem: '#LAY_qrcodeUpload'
        , auto: true
        , choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: '/admin/user/signForCos'
                , success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime
                                });
                            }
                        });
                        cos.putObject({
                            Bucket: res.data.bucket,
                            Region: res.data.region,
                            Key: 'images/' + res.data.filename + choosefile.name,
                            Body: choosefile,
                        }, function (err, data) {
                            layer.closeAll('loading');
                            if (err) {
                                layer.msg("上传失败", { icon: 5 });
                            } else {
                                if (data.statusCode == 200) {
                                    qrcodeSrc.val("https://" + data.Location);
                                } else {
                                    layer.msg("图片上传失败", { icon: 5 });
                                }
                            }
                        });
                    } else {
                        layer.closeAll('loading');
                        layer.msg(res.msg, { icon: 5 });
                    }
                }
            })
        }
    });

    //查看头像
    admin.events.qrcodePreview = function (othis) {
        var src = qrcodeSrc.val();
        layer.photos({
            photos: {
                "title": "查看" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    //add:新增 微信、微博、抖音 二维码图片上传
    initUploader('LAY_wx_pub_upload', 'LAY_wx_pub_src', 'LAY_wx_pub_preview');
    initUploader('LAY_weibo_upload', 'LAY_weibo_src', 'LAY_weibo_preview');
    initUploader('LAY_douyin_upload', 'LAY_douyin_src', 'LAY_douyin_preview');

    //上传图片
    var iconSrc = $("#LAY_iconImgSrc");
    upload.render({
        url: "/admin/index/noOperation",
        elem: "#LAY_iconImgUpload",
        auto: true,
        choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: "/admin/live/signForCos",
                success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime,
                                });
                            },
                            Domain: res.data.imgDomain,
                        });
                        cos.putObject(
                            {
                                Bucket: res.data.bucket,
                                Region: res.data.region,
                                Key: "images/" + res.data.filename + choosefile.name,
                                Body: choosefile,
                            },
                            function (err, data) {
                                layer.closeAll("loading");
                                if (err) {
                                    console.log("上传失败", err);
                                    layer.msg("上传失败", { icon: 5 });
                                } else {
                                    if (data.statusCode == 200) {
                                        iconSrc.val("https://" + data.Location);
                                    } else {
                                        layer.msg("图片上传失败", { icon: 5 });
                                    }
                                }
                            }
                        );
                    } else {
                        layer.closeAll("loading");
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        },
    });
    //上传图片
    var iconSrc1 = $("#LAY_iconImgSrc1");
    upload.render({
        url: "/admin/index/noOperation",
        elem: "#LAY_iconImgUpload1",
        auto: true,
        choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: "/admin/live/signForCos",
                success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime,
                                });
                            },
                            Domain: res.data.imgDomain,
                        });
                        cos.putObject(
                            {
                                Bucket: res.data.bucket,
                                Region: res.data.region,
                                Key: "images/" + res.data.filename + choosefile.name,
                                Body: choosefile,
                            },
                            function (err, data) {
                                layer.closeAll("loading");
                                if (err) {
                                    console.log("上传失败", err);
                                    layer.msg("上传失败", { icon: 5 });
                                } else {
                                    if (data.statusCode == 200) {
                                        iconSrc1.val("https://" + data.Location);
                                    } else {
                                        layer.msg("图片上传失败", { icon: 5 });
                                    }
                                }
                            }
                        );
                    } else {
                        layer.closeAll("loading");
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        },
    });
    //上传图片
    var iconSrc2 = $("#LAY_iconImgSrc2");
    upload.render({
        url: "/admin/index/noOperation",
        elem: "#LAY_iconImgUpload2",
        auto: true,
        choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: "/admin/live/signForCos",
                success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime,
                                });
                            },
                            Domain: res.data.imgDomain,
                        });
                        cos.putObject(
                            {
                                Bucket: res.data.bucket,
                                Region: res.data.region,
                                Key: "images/" + res.data.filename + choosefile.name,
                                Body: choosefile,
                            },
                            function (err, data) {
                                layer.closeAll("loading");
                                if (err) {
                                    console.log("上传失败", err);
                                    layer.msg("上传失败", { icon: 5 });
                                } else {
                                    if (data.statusCode == 200) {
                                        iconSrc2.val("https://" + data.Location);
                                    } else {
                                        layer.msg("图片上传失败", { icon: 5 });
                                    }
                                }
                            }
                        );
                    } else {
                        layer.closeAll("loading");
                        layer.msg(res.msg, { icon: 5 });
                    }
                },
            });
        },
    });

    exports('setting', {})
});

function initUploader(btnElem, inputElem, btnPreview) {
    var $ = layui.$
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    var qrcodeSrc = $('#' + inputElem);
    upload.render({
        url: '/admin/index/noOperation'
        , elem: '#' + btnElem
        , auto: true
        , choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: '/admin/user/signForCos'
                , success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime
                                });
                            }
                        });
                        cos.putObject({
                            Bucket: res.data.bucket,
                            Region: res.data.region,
                            Key: 'images/' + res.data.filename + choosefile.name,
                            Body: choosefile,
                        }, function (err, data) {
                            layer.closeAll('loading');
                            if (err) {
                                layer.msg("上传失败", { icon: 5 });
                            } else {
                                if (data.statusCode == 200) {
                                    qrcodeSrc.val("https://" + data.Location);
                                } else {
                                    layer.msg("图片上传失败", { icon: 5 });
                                }
                            }
                        });
                    } else {
                        layer.closeAll('loading');
                        layer.msg(res.msg, { icon: 5 });
                    }
                }
            })
        }
    });
    //preview
    $('#' + btnPreview).on('click', function () {
        var src = qrcodeSrc.val();
        layer.photos({
            photos: {
                "title": "查看" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    });
}